package io.renren.modules.gaokaobaobei.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.gaokaobaobei.entity.AgencyEntity;
import io.renren.modules.gaokaobaobei.service.AgencyService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysDeptEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的招生代理
 *
 * @author zlb
 * @email 316682546@qq.com
 * @date 2018-11-05 09:50:02
 */
@RestController
@RequestMapping("gaokaobaobei/agency")
public class AgencyController extends AbstractController {
    @Autowired
    private AgencyService agencyService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表
     */
    @RequestMapping("/getMyBaobeiList")
   // @RequiresPermissions("gaokaobaobei:agency:list")
    public R getMyBaobeiList(@RequestParam Map<String, Object> params){
        SysUserEntity sysUserEntity=getUser();
        params.put("username",sysUserEntity.getUsername());
         PageUtils page = agencyService.getMyBaobeiList(params);

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/getBaobeiList")
    // @RequiresPermissions("gaokaobaobei:agency:list")
    public R getBaobeiList(@RequestParam Map<String, Object> params){
//        SysUserEntity sysUserEntity=getUser();
//        params.put("username",sysUserEntity.getUsername());
        PageUtils page = agencyService.getMyBaobeiList(params);

        return R.ok().put("page", page);
    }





    /**
     * 统计业绩
     */
    @RequestMapping("/countOutstanding")
    public R countOutstanding(@RequestBody String username){
        List countOutstanding = agencyService.countOutstanding(username);
        return R.ok().put("countOutstanding", countOutstanding);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
 //   @RequiresPermissions("gaokaobaobei:agency:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("type","夏考");
            PageUtils page = agencyService.queryPage(params);

        return R.ok().put("page", page);
    }
    @RequestMapping("/getNumber2")
    public R getNumber2(){
        int userNumber = agencyService.userCount()-1;//招生总人数
        int wati = agencyService.performanceNumber("单招");//等待审核
        int pass = agencyService.checkedNumber("单招");//已审核
        String msg = "招生人员:<font color='green'>"+userNumber+"</font>;单招招生业绩:[<font color='red'> "+wati+"</font> ]人 等待审核; [<font color='green'> "+pass+" </font>]人 已审核";
        return R.ok().put("msg",msg);
    }
    @RequestMapping("/getNumber1")
    public R getNumber1(){
        int userNumber = agencyService.userCount()-1;//招生总人数
        int wati = agencyService.performanceNumber("夏考");//等待审核
        int pass = agencyService.checkedNumber("夏考");//已审核
        String msg = "招生人员:<font color='green'>"+userNumber+"</font>;夏考招生业绩:[<font color='red'> "+wati+"</font> ]人 等待审核; [<font color='green'> "+pass+" </font>]人 已审核";
        return R.ok().put("msg",msg);
    }

    @RequestMapping("/getNumberkaoyan")
    public R getNumberkaoyan(){
        int userNumber = agencyService.userCount()-1;//招生总人数
        int wati = agencyService.performanceNumber("考研");//等待审核
        int pass = agencyService.checkedNumber("考研");//已审核
        String msg = "招生人员:<font color='green'>"+userNumber+"</font>;考研招生业绩:[<font color='red'> "+wati+"</font> ]人 等待审核; [<font color='green'> "+pass+" </font>]人 已审核";
        return R.ok().put("msg",msg);
    }

    @RequestMapping("/getNumbershengben")
    public R getNumbershengben(){
        int userNumber = agencyService.userCount()-1;//招生总人数
        int wati = agencyService.performanceNumber("升本");//等待审核
        int pass = agencyService.checkedNumber("升本");//已审核
        String msg = "招生人员:<font color='green'>"+userNumber+"</font>;升本招生业绩:[<font color='red'> "+wati+"</font> ]人 等待审核; [<font color='green'> "+pass+" </font>]人 已审核";
        return R.ok().put("msg",msg);
    }


    /**
     * 列表
     */
    @RequestMapping("/list2")
    public R list2(@RequestParam Map<String, Object> params){
        params.put("type","单招");
        PageUtils page = agencyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("gaokaobaobei:agency:info")
    public R info(@PathVariable("id") Integer id){
        AgencyEntity agency = agencyService.selectById(id);

        return R.ok().put("agency", agency);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("gaokaobaobei:agency:save")
    public R save(@RequestBody AgencyEntity agency){
        SysUserEntity sysUserEntity=sysUserService.selectById(agency.getUserId());
        agency.setUnit(sysUserEntity.getDeptName());
        agency.setPhone(sysUserEntity.getMobile());
        if(agency.getUserId()!=null) {
            boolean flag = agencyService.insert(agency);
            return R.ok();
        }else {
            return R.error(101,"新用户未选择");
        }
        //代理人添加成功后 添加 该代理人到用户表
//       if(flag){
//           SysUserEntity user = new SysUserEntity();
//           List list = new ArrayList();
//           list.add(2L);
//           user.setUsername(newUserName);
//           user.setPassword(newPassWord);
//           user.setEmail("zte@zte.com");
//           user.setMobile(agency.getPhone());
//           user.setStatus(1);
//           user.setRoleIdList(list);
//           user.setDeptId(18L);
//           user.setDeptName("代理人");
//           ValidatorUtils.validateEntity(user, AddGroup.class);
//           sysUserService.save(user);
//       }


    }

    /**
     * 保存代理人姓名用于查询

     */
    @RequestMapping("/updateDaili")
    public R updateDaili(@RequestBody AgencyEntity agency){
        agencyService.updateDaili(agency);
        return R.ok();
    }

    /**
     * 保存代理人姓名用于查询
     * @param name 代理人姓名
     * @param type null？
     * @param Admissionstype 是夏考 还是单招
     * @param session  临时存入session
     * @return
     */
    @RequestMapping("/saveName")
    public R saveName(String name, int type,String Admissionstype, HttpSession session){
        session.setAttribute("name",name);
        session.setAttribute("type",type);
        session.setAttribute("Admissionstype",Admissionstype);
        return R.ok();
    }






    @RequestMapping("/getNumber")
    public R getNumber1(HttpSession session){
        String name = session.getAttribute("name")+"";//name
        int type = (Integer) session.getAttribute("type");
        String Admissionstype = type==0?"夏考":"单招";
        Map map = new HashMap();
        map.put("name",name);
        map.put("Admissionstype",Admissionstype);
        int wati = agencyService.userPerformanceNumber(map);//等待审核
        int pass = agencyService.userCheckedNumber(map);//审核通过
        String dailiNumber = agencyService.dailiNumber(map);
        if(dailiNumber ==null){
            dailiNumber = "0";
        }
        String msg = "招生人员:<font color='green'>"+name+"</font>;审核通过:[<font color='red'> "+pass+"</font> ]人 等待审核: [<font color='red'> "+wati+" </font>]人  代理人共"+dailiNumber+"人 通过审核";
        return R.ok().put("msg",msg);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("gaokaobaobei:agency:update")
    public R update(@RequestBody AgencyEntity agency){
        ValidatorUtils.validateEntity(agency);
        agencyService.updateAllColumnById(agency);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("gaokaobaobei:agency:delete")
    public R delete(@RequestBody Integer[] ids){
        agencyService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 查询代理人总数
     * @param name
     * @param type
     * @return
     */
    @RequestMapping("/selectNumber")
    public R selectNumber(String name,String type){
        Map<String ,String > map = new HashMap();
        map.put("superior",name);
        map.put("type",type);
       Long number  = agencyService.selectPersonNumber(map);

        return R.ok().put("number",number);
    }

    /**
     * 查询所有代理人总业绩
     * @param name
     * @param type
     * @return
     */
    @RequestMapping("/selectSum")
    public R selectSum(String name ,String type){
        Map<String ,String > map = new HashMap();
        map.put("superior",name);
        map.put("type",type);
        Object s =  agencyService.selectSum(map);
       int sum = 0;
       if (s!=null){
           sum = (Integer)s;
       }
        return R.ok().put("sum",sum);
    }

    /**
     * 列表
     */
    @RequestMapping("/getNewUserTree")
    public List<SysDeptEntity> list(){
        List<SysDeptEntity> deptList = agencyService.getNewUserTree();

        return deptList;
    }


    /**
     * 列表
     */
    @RequestMapping("/getUserTree")
    public List<SysDeptEntity> getUserTree(){
        List<SysDeptEntity> deptList = agencyService.getUserTree();

        return deptList;
    }


}
